﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SBPweb.MediaCenter.Tools.WPF.Resources
{
    public class ResourceManager
    {

        public static ImageSource ExtractJpegAsSource(Assembly asm, string name)
        {
            System.IO.Stream fileStream = asm.GetManifestResourceStream(name);
            if (fileStream != null)
            {
                JpegBitmapDecoder bitmapDecoder = new JpegBitmapDecoder(fileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                ImageSource imageSource = bitmapDecoder.Frames[0];
                return imageSource;
            }

            return null;
        }

        public static BitmapImage ExtractJpegAsImage(Assembly asm, string name)
        {
            System.IO.Stream fileStream = asm.GetManifestResourceStream(name);
            if (fileStream != null)
            {
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = fileStream;
                bitmapImage.EndInit();
                return bitmapImage;
            }

            return null;
        }

    }
}
